<template>
  <el-dialog
    :visible="showPermission"
    title="分配权限"
    @close="hClose"
  >
    <el-tree
      ref="tree"
      node-key="id"
      :data="PermissionList"
      :props="{label:'name'}"
      show-checkbox
      check-strictly
      default-expand-all
    />
    <el-button type="primary" @click="hSave">保存</el-button>
    <el-button @click="hClose">取消</el-button>
  </el-dialog>
</template>

<script>
import { getPermission } from '@/api/permission'
import { getRoleDetail, setAssignPrem } from '@/api/role'
import { arr2tree } from '@/utils/index'
export default {
  props: {
    showPermission: {
      type: Boolean,
      required: true
    },
    curId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      PermissionList: []
    }
  },
  created() {
    this.getPremissionList()
  },
  methods: {
    async  hSave() {
      const keys = this.$refs.tree.getCheckedKeys()
      await setAssignPrem({ id: this.curId, permIds: keys })
      this.$message.success('保存成功')
      this.hClose()
    },
    async getRolePrem() {
      const res = await getRoleDetail(this.curId)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    hClose() {
      this.$emit('update:showPermission', false)
    },
    async getPremissionList() {
      const res = await getPermission()
      this.PermissionList = arr2tree(res.data, 0)
    }
  }
}
</script>

<style>

</style>
